package com.xz.group12306.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 胡晨倩
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		   String contentType=request.getContentType();
		   String servername=request.getServerName();
		   String realpath=request.getRealPath(servername);
		  // System.out.println(contentType);

		   InputStream in=null;
		   OutputStream out=null;
		   if(contentType.indexOf("multipart/form-data")>=0){
		    in=request.getInputStream();
		    int formlength=request.getContentLength();
		    byte[] formcontent=new byte[formlength];
		    int totalread=0;
		    int nowread=0;
		    while(totalread<formlength){
		     nowread=in.read(formcontent,totalread, formlength);
		     totalread+=nowread;
		    }
		    String strcontent=new String(formcontent);
		   // System.out.println(strcontent);
		    int typestart=strcontent.indexOf("Content-Type:")+14;
		    int typeend=strcontent.indexOf("\n", typestart)-1;
		    String formType=strcontent.substring(typestart, typeend);
		   if(formType.equals("image/jpeg")||formType.equals("image/gif")||formType.equals("image/pjepg")){
		     int filenamestart=strcontent.indexOf("filename=\"")+10;
		     int filenameend=strcontent.indexOf("\n",filenamestart)-2;
		     String filename=strcontent.substring(filenamestart,filenameend);
		    filename=filename.substring(filename.lastIndexOf("."));
		     String newfilename=""+(new Date()).getDate()+(new Date()).getHours()+(new Date()).getMinutes()+(new Date()).getSeconds();
		     newfilename=newfilename+filename;
		     realpath=realpath+"";
		     newfilename=realpath+newfilename;
		     int filestart=strcontent.indexOf("\n",typestart)+1;
		    filestart=strcontent.indexOf("\n",filestart)+1;
		     int intboundary=contentType.indexOf("boundary=")+10;
		     String strboundary=contentType.substring(intboundary);
		     int fileend=strcontent.indexOf(strboundary,filestart)-4;
		     String saveFile=strcontent.substring(filestart,fileend);
		     int contentstart=strcontent.substring(0,filestart).getBytes().length;
		     int contentend=strcontent.substring(0,fileend).getBytes().length;
		    // System.out.println(saveFile);
		     File myfile=new File(realpath);
		     if(!myfile.exists()){
		      myfile.mkdirs();
		     }
		     out=new FileOutputStream(newfilename);
		     out.write(formcontent, contentstart,contentend-contentstart);
		     System.out.println(newfilename+"\thhhh"+realpath);
		     System.out.println("成功啦");
		    
		     HttpSession session=request.getSession();
				session.setAttribute("newfilename", newfilename);
		     response.sendRedirect("user/doUploadImage.jsp");
		     
		    }else{
		    	System.out.println("出错");
		     response.sendRedirect("error.jsp");
		    }
		   }
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
